Python Program to Print All Prime Numbers
In our previous tutorial, you have learned to check if a number is prime number. You just need to use code inside a for loop.
Example:
A sample Python program to take input of maximum number and print all available prime numbers between 1 and given number.
1 2 3 4 5 6 7 8 9 | max = int(input("Enter maximum number: ")) for num in range(1,max + 1): if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num) |
Ouput:
Enter max number: 20 2 3 5 7 11 13 17 19
Another Example:
Another example to take input of minimum and maximum number. Then print all the prime numbers available between those numbers
1 2 3 4 5 6 7 8 9 10 | min = int(input("Enter start number: ")) max = int(input("Enter end number: ")) for num in range(min,max + 1): if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num) |
Output:
Enter start number: 50 Enter end number: 100 53 59 61 67 71 73 79 83 89 97